package com.example.testapp.activity.activity.read;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.model.ChatItemListViewBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by l on 2016/7/31.
 */
public class ChatItemListTestActivity extends AppCompatActivity {

    private ListView  mListView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_chat_list);
        initView();
    }

    private void initView() {
        mListView = (ListView)findViewById(R.id.listView_chat);
        ChatItemListViewBean  bean1 = new ChatItemListViewBean();
        bean1.setType(0);
        bean1.setIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.in_icon));
        bean1.setText("Hello how are you?");

        ChatItemListViewBean  bean2 = new ChatItemListViewBean();
        bean2.setType(1);
        bean2.setIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.in_icon));
        bean2.setText("Fine thank you, and you?");

        ChatItemListViewBean  bean3 = new ChatItemListViewBean();
        bean3.setType(0);
        bean3.setIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.in_icon));
        bean3.setText("I am fine too");

        ChatItemListViewBean  bean4 = new ChatItemListViewBean();
        bean4.setType(1);
        bean4.setIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.in_icon));
        bean4.setText("Bye bye");

        ChatItemListViewBean  bean5 = new ChatItemListViewBean();
        bean5.setType(0);
        bean5.setIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.in_icon));
        bean5.setText("See you");

        List<ChatItemListViewBean> data = new ArrayList<>();
        data.add(bean1);
        data.add(bean2);
        data.add(bean3);
        data.add(bean4);
        data.add(bean5);
        mListView.setAdapter(new ChatItemListViewAdapter(this, data));
    }

    public class ChatItemListViewAdapter extends BaseAdapter{

        private List<ChatItemListViewBean> mData;
        private LayoutInflater  mInflater;

        public ChatItemListViewAdapter(Context context, List<ChatItemListViewBean> data) {
            this.mData = data;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {
            ChatItemListViewBean bean = mData.get(position);
            return bean.getType();
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                if(getItemViewType(position) == 0){
                    holder = new ViewHolder();
                    convertView = mInflater.inflate(R.layout.view_list_chatin, null);
                    holder.icon = (ImageView) convertView.findViewById(R.id.icon_in);
                    holder.text = (TextView) convertView.findViewById(R.id.text_in);
                }else{
                    holder = new ViewHolder();
                    convertView = mInflater.inflate(R.layout.view_list_chatout, null);
                    holder.icon = (ImageView) convertView.findViewById(R.id.icon_out);
                    holder.text = (TextView) convertView.findViewById(R.id.text_out);
                }
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            holder.icon.setImageBitmap(mData.get(position).getIcon());
            holder.text.setText(mData.get(position).getText());
            return convertView;
        }

        public class ViewHolder{
            ImageView icon;
            TextView  text;
        }
    }
}
